<template>
  <div>
    <TopFilter :list="formList1" :rows="2" labelWidth="100" :collapse="true" @filterChange="formchangeFun"></TopFilter>
    <!-- <FormPanel ref="formpanel1" :list="formList1" labelWidth="100" :isSubmitBtn="false" @formChange="formchangeFun" /> -->
    <!-- <ButtonArea :style="{ paddingLeft: '100px' }">
      <el-button type="primary" @click="FormPanelSeacher">搜 索</el-button>
      <el-button @click="FormPanelreset">重置</el-button>
    </ButtonArea> -->
    <FilterTable
      ref="basetable"
      columnsRef="partPartOtherPartObliTabsAdd"
      :columns="columns"
      height="auto"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'rows'"
      :onColumnsChange="columns => (this.columns = columns)"
      :dataSource="datasourceTablefirst"
      :onEnterEvent="tableDbclickFun"
      selectionType="single"
      :isSelectColumn="false"
      :onRowSelectChange="selectTableFun"
    ></FilterTable>
    <FormPanel ref="formpanel2" :list="formList2" labelWidth="100" :isSubmitBtn="false" @formChange="formchangeFuns" />
    <FilterTable
      ref="basetables"
      columnsRef="partPartOtherPartObliTabsAdd1"
      :isSelectColumn="false"
      :columns="columns1"
      :dataSource="datasourceTablesecond"
      :params="fetchParams"
      :datakey="'rows'"
      :onColumnsChange="columns1 => (this.columns1 = columns1)"
      height="auto"
      :onSyncTableData="tablechangeFun"
    ></FilterTable>
    <div class="btnWrap">
      <el-button @click="closeDialog">取消</el-button>
      <el-button type="primary" @click="confirmHandle">确定</el-button>
    </div>
  </div>
</template>
<script>
import { queryPart, queryPartItemInfos, getStroageList } from '@/api/part/partOutbound/partBreak';
import { dictionary } from '@/utils/dictMixin';
import { isAuthorized } from '@/utils/index';
import { getPinpSelect } from '@/api/customerRepair/customerCare/activitiesInvite/index';
import { findAllRepair, getBrand } from '@/api/repair/basicParameters';
import moment from 'moment';
export default {
  mixins: [dictionary],
  props: ['lossNo'],
  data() {
    lossNo: this.lossNo, (this.basetable = null);
    this.basetables = null;
    return {
      formList1: this.createFrom(),
      formList2: this.createFroms(),
      fetchApi: queryPart,
      fetchParams: {
        xhrAbort: true
        // sort: '',
        // order: 'asc',
        // limit: '10',
        // offset: '0',
        // queryPart_table: '',
        // dmsFuncId: '35100010',
        // _: '1565683718279'
      },
      getutildata: {
        dmsFuncId: '35100010',
        _: '1565683718279'
      },
      columns: this.createTable(),
      columns1: this.createTables(),
      datasourceTablefirst: [],
      datasourceTablesecond: [],
      tablelistfirst: [],
      tablelistsecond: [],
      getpartParams: {
        sort: 'STORAGE_CODE',
        order: 'asc',
        limit: '10',
        offset: '0',
        queryPart_table: '',
        STORAGE_CODE: '',
        PART_NO: ''
      },
      fromSubmitdata: {}
    };
  },
  created() {},
  mounted() {
    this.initSelect();
    this.getBrand();
    this.basetable = this.$refs.basetable.$refs.pageTable;
    this.basetables = this.$refs.basetables.$refs.pageTable;
  },
  methods: {
    // 品牌
    async getBrand() {
      const res = await getBrand();
      this.formList1.find(item => item.fieldName == 'brand').itemList = res.data.map(x => ({ text: x.BRAND_NAME, value: x.BRAND_CODE + '' }));
      this.formList1.find(x => x.fieldName === 'brand').initialValue = res.data.find(item => item.BRAND_NAME == '奥迪').BRAND_CODE + '';
    },
    async initSelect() {
      // 仓库
      const res = await getStroageList();
      this.formList1.find(item => item.fieldName === 'storageCode').itemList = res.data.map(x => ({ value: x.STORAGE_CODE, text: x.STORAGE_NAME }));
      this.formList2.find(item => item.fieldName === 'STORAGE_CODE').itemList = res.data.map(x => ({ value: x.STORAGE_CODE, text: x.STORAGE_NAME }));
      this.columns.find(item => item.dataIndex === 'storageCode').editItems = res.data.map(x => ({ value: x.STORAGE_CODE, text: x.STORAGE_NAME }));
      // 品牌
      // this.formList1.find(item => item.fieldName === 'brand').itemList = (await getPinpSelect()).data.map(x => ({ value: x.brandCode, text: x.brandName }));
      // 默认仓库赋值
      // let resdata = await findAllRepair();
      // this.formList1.find(item => item.fieldName === 'storageCode').initialValue = resdata.data['1100'];
    },
    checkPriceRate(rule, value, callback) {
      let reg = /^([1-9])(\.\d{1,2})?$/;
      if (!reg.test(value)) {
        return callback(new Error(rule.message));
      }
      callback();
    },
    checkZero(rule, value, callback) {
      if (value <= rule.limit) {
        return callback(new Error(rule.message));
      }
      callback();
    },
    createFrom() {
      return [
        {
          type: 'SELECT',
          label: '仓库',
          fieldName: 'storageCode',
          itemList: []
        },
        {
          type: 'SELECT',
          label: '品牌',
          fieldName: 'brand',
          itemList: []
        },
        {
          type: 'SELECT',
          label: '配件类别',
          fieldName: 'partGroupCode',
          itemList: this.createDictList('8075')
        },
        {
          type: 'INPUT',
          label: '配件代码',
          fieldName: 'partNo'
        },
        {
          type: 'INPUT',
          label: '拼音',
          fieldName: 'spellCode'
        },
        {
          type: 'INPUT',
          label: '配件名称',
          fieldName: 'partName'
        },
        {
          type: 'INPUT',
          label: '库位',
          fieldName: 'storagePositionCode'
        },
        {
          type: 'SELECT',
          label: '可用库存大于零',
          fieldName: 'stockZero',
          initialValue: 10041001,
          itemList: [
            { text: '是', value: 10041001 },
            { text: '否', value: 10041002 }
          ]
        },
        {
          type: 'SELECT',
          label: '售价大于零',
          fieldName: 'priceZero',
          itemList: [
            { text: '是', value: 10041001 },
            { text: '否', value: 10041002 }
          ]
        },
        {
          type: 'INPUT',
          label: '备注',
          fieldName: 'remark'
        }
      ];
    },
    createTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '仓库代码',
          dataIndex: 'storageCode',
          sorter: true,
          filter: true,
          filterType: 'input'
          // editable: false,
          // editType: 'select',
          // editItems: []
        },
        {
          title: '仓库名称',
          dataIndex: 'storageName',
          sorter: true,
          filter: true,
          filterType: 'input'
          // editable: false,
          // editType: 'select',
          // editItems: []
        },
        {
          title: '库位',
          dataIndex: 'storagePositionCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '配件代码',
          dataIndex: 'partNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '配件名称',
          dataIndex: 'partName',
          showOverflowTooltip: true,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '含税销售价',
          dataIndex: 'costSalesPrice',
          precision: 2,
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '不含税成本单价',
          dataIndex: 'costPrice',
          precision: 2,
          sorter: true,
          filter: true,
          filterType: 'number',
          hidden: !isAuthorized('1313', '13131005')
        },
        {
          title: '可用库存',
          dataIndex: 'useableStock',
          precision: 2,
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '账面库存',
          dataIndex: 'stockQuantity',
          precision: 2,
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '备注',
          dataIndex: 'remark',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        // {
        //   title: '仓库名称',
        //   dataIndex: 'storageName',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input'
        // },
        {
          title: '配件类别',
          dataIndex: 'partGroupCode',
          sorter: true,
          render: props => {
            return <span>{this.createDictText(props.row.partGroupCode, '8075')}</span>;
          }
        }
      ];
    },
    createFroms() {
      return [
        {
          type: 'SELECT',
          label: '仓库',
          fieldName: 'STORAGE_CODE',
          itemList: [],
          initialValue: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '配件代码',
          fieldName: 'PART_NO',
          initialValue: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '配件名称',
          fieldName: 'PART_NAME',
          initialValue: '',
          rules: [{ required: true, message: '请输入配件名称', trigger: 'blur' }]
        },
        {
          type: 'INPUT_NUMBER',
          label: '预留数量',
          fieldName: 'QUANTITY',
          precision: 2,
          initialValue: 0.0,
          min: 0,
          max: 9999999
        }
      ];
    },
    createTables() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '含税销售单价',
          dataIndex: 'COST_SALES_PRICE',
          precision: 2,
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '不含税成本单价',
          dataIndex: 'COST_PRICE',
          precision: 2,
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '不含税成本金额',
          dataIndex: 'COST_AMOUNT',
          precision: 2,
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '账面库存',
          dataIndex: 'STOCK_QUANTITY',
          precision: 2,
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '可用库存',
          dataIndex: 'USEABLE_STOCK',
          precision: 2,
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '锁定量',
          dataIndex: 'LOCKED_QUANTITY',
          precision: 2,
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '不含税建议销售价',
          dataIndex: 'REFER_PRICE',
          precision: 2,
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '最大库存',
          dataIndex: 'MAX_STOCK',
          precision: 2,
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '最小库存',
          dataIndex: 'MIN_STOCK',
          precision: 2,
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '单位代码',
          dataIndex: 'UNIT_CODE',
          sorter: true,
          filter: true,
          hidden: true,
          filterType: 'input'
        },
        {
          title: '单位名称',
          dataIndex: 'UNIT_NAME',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '索赔价',
          dataIndex: 'CLAIM_PRICE',
          precision: 2,
          sorter: true,
          filter: true,
          filterType: 'number',
          hidden: !isAuthorized('1313', '13131006')
        },
        {
          title: '终端建议零售价格',
          dataIndex: 'INSURANCE_PRICE',
          precision: 2,
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '销售限价',
          dataIndex: 'LIMIT_PRICE',
          precision: 2,
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '网点价',
          dataIndex: 'NODE_PRICE',
          precision: 2,
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '最新进货价',
          dataIndex: 'LATEST_PRICE',
          precision: 2,
          sorter: true,
          filter: true,
          filterType: 'number'
        }
      ];
    },
    // 重置
    FormPanelreset() {
      this.$refs.formpanel1.RESET_FORM();
    },
    FormPanelSeacher() {
      this.$refs.formpanel1.SUBMIT_FORM();
    },
    // 选中事件
    selectTableFun(val) {
      if (val && val.length > 0) {
      }
    },
    formchangeFun(val) {
      // this.fetchParams.xhrAbort = true;
      // this.fetchParams = Object.assign(this.fetchParam, val, { xhrAbort: false });
      this.fetchParams = Object.assign({}, val, { xhrAbort: false });
    },
    formchangeFuns(val) {
      this.fromSubmitdata = val;
    },
    tableDbclickFun(val) {
      this.datasourceTablesecond = [];
      this.tablelistfirst = val;
      this.formList2.find(item => item.fieldName === 'STORAGE_CODE').initialValue = val.storageCode;
      this.formList2.find(item => item.fieldName === 'PART_NO').initialValue = val.partNo;
      this.formList2.find(item => item.fieldName === 'PART_NAME').initialValue = val.partName;
      this.getpartlist(val);
    },
    async getpartlist(val) {
      let res = await queryPartItemInfos({ partNo: val.partNo, storageCode: val.storageCode });
      this.formList2.find(item => item.fieldName === 'QUANTITY').initialValue = 1;
      this.basetables.EXECUTE_INSERT(res.data.rows);
      this.$nextTick(this.confirmHandle);
    },
    // 关闭弹窗
    closeDialog() {
      this.$emit('close', false);
    },
    tablechangeFun(val) {
      this.tablelistsecond = val;
    },
    // 添加事件提交表单
    async confirmHandle() {
      this.$refs.formpanel2.SUBMIT_FORM();
      if (!this.tablelistsecond[0]) {
        return this.$notify.error({ title: '提示信息', message: '请选择配件!' });
      }
      console.log(this.tablelistsecond, '表单数据');
      if (this.tablelistsecond[0].USEABLE_STOCK <= 0) {
        return this.$notify.error({ title: '提示信息', message: '添加失败!当前配件可用库存数量不足' });
      }
      // 编辑部分
      let json = {
        storageName: this.tablelistfirst.STORAGENAME,
        storagePositionCode: this.tablelistfirst.storagePositionCode,
        unitCode: this.tablelistsecond[0].UNIT_CODE,
        unitName: this.tablelistsecond[0].UNIT_NAME,
        costPrice: this.tablelistsecond[0].COST_PRICE,
        limitPrice: this.tablelistsecond[0].LIMIT_PRICE,
        costAmount: Number(this.tablelistsecond[0].COST_PRICE) * Number(this.fromSubmitdata.QUANTITY ? this.fromSubmitdata.QUANTITY : 1),
        isObligated: 10041001,
        useableStock: this.tablelistsecond[0].USEABLE_STOCK,
        quantoty: this.formList2.find(item => item.fieldName === 'QUANTITY').initialValue,
        partNo: this.fromSubmitdata.PART_NO,
        partName: this.fromSubmitdata.PART_NAME,
        storageCode: this.fromSubmitdata.STORAGE_CODE,
        quantity: this.fromSubmitdata.QUANTITY ? this.fromSubmitdata.QUANTITY : 1,
        bookingDate: moment(new Date()).format('YYYY-MM-DD')
      };
      if (+this.tablelistsecond[0].USEABLE_STOCK < +this.fromSubmitdata.QUANTITY) {
        return this.$notify.error({ title: '提示信息', message: '出库数量不能大于可用库存!' });
      }
      let formdata = Object.assign({}, json);
      if (formdata.partNo === '' || formdata.storageCode === '') {
        return this.$notify.error({ title: '提示信息', message: '请选择配件!' });
      }
      this.$emit('getAddData', false, formdata);
    }
  }
};
</script>

<style scoped>
.btnWrap {
  position: absolute;
  left: 0px;
  bottom: 0px;
  right: 0px;
  z-index: 9;
  border-top: 1px solid rgb(233, 233, 233);
  padding: 10px 20px;
  background: rgb(255, 255, 255);
  text-align: right;
}
</style>
